const axios = require('axios')
const ffi = require('ffi-napi');
const ref = require('ref-napi');

const dll_path = "E:\\workspace\\Cpp\\FHESearchOnEncryptedData\\x64\\Debug"
const dll = ffi.Library(`${dll_path}/FHESED4Customer.dll`, {
    dll_example: [ref.types.int, [ref.types.int]],
    read_aes_ecb_sk: [ref.refType(ref.types.char), [ref.refType(ref.types.char), ref.refType(ref.types.char), ref.refType(ref.types.int)]],
    aes_ecb_enc_utf8str: [ref.refType(ref.types.char), [ref.refType(ref.types.char), ref.refType(ref.types.char), ref.refType(ref.types.int)]],
    aes_ecb_dec_utf8str: [ref.refType(ref.types.char), [ref.refType(ref.types.char), ref.refType(ref.types.char), ref.refType(ref.types.int)]],
    read_soft_enc_pk: [ref.refType(ref.types.char), [ref.refType(ref.types.char), ref.refType(ref.types.char), ref.refType(ref.types.int)]],
    fhe_soft_enc_utf8str: [ref.refType(ref.types.char), [ref.refType(ref.types.char), ref.refType(ref.types.char), ref.refType(ref.types.int)]],
});

// read_soft_enc_pk
const fhe_pk_file_path = "E:\\workspace\\Cpp\\FHESearchOnEncryptedData\\key\\huangheng20231023_pk_key.txt"
const fhe_pk_file_path_buf = ref.allocCString(fhe_pk_file_path);
const read_soft_enc_pk_res = Buffer.alloc(100);
const read_soft_enc_pk_res_length = ref.alloc(ref.types.int);
dll.read_soft_enc_pk(fhe_pk_file_path_buf, read_soft_enc_pk_res, read_soft_enc_pk_res_length);
console.log("read_soft_enc_pk_res", read_soft_enc_pk_res.toString())
console.log("read_soft_enc_pk_res_length", read_soft_enc_pk_res_length.deref())

// fhe_soft_enc_utf8str
const fhe_soft_enc_utf8str_param = "亚里士多德"
const fhe_soft_enc_utf8str_param_buf = ref.allocCString(fhe_soft_enc_utf8str_param);
const fhe_soft_enc_utf8str_res = Buffer.alloc(256*fhe_soft_enc_utf8str_param.length+1);
const fhe_soft_enc_utf8str_res_length = ref.alloc(ref.types.int);
dll.fhe_soft_enc_utf8str(fhe_soft_enc_utf8str_param_buf, fhe_soft_enc_utf8str_res, fhe_soft_enc_utf8str_res_length);
console.log("fhe_soft_enc_utf8str_res", fhe_soft_enc_utf8str_res.toString())
console.log("fhe_soft_enc_utf8str_res_length", fhe_soft_enc_utf8str_res_length.deref())
const utf8str_enc = fhe_soft_enc_utf8str_res.toString()

const encrypt_cloud_host = "http://127.0.0.1:5020"
const encrypt_serves = axios.create({
  baseURL: encrypt_cloud_host,
  // timeout: 5000
})

encrypt_serves({
    url: 'hello',
    method: 'get',
    params: {
        num: 13
    }
}).then(res => {
    console.log(res.data)
    // console.log(res)
}).catch(err => {
    console.log(err)
})


encrypt_serves({
    url: 'encrypt_search',
    method: 'post',
    data:{
        enc_keyword: utf8str_enc
    }
}).then(res => {
    // console.log(res.data)
    console.log(res.data.doc_match_array.length)
}).catch(err => {
    console.log(err)
})